package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.mapper.orderMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.impl.OrderMannerServiceImpI;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
@Slf4j
@RestController
@RequestMapping("/admin/order")
@Api(tags = "商家订单管理")
public class OrderMannerController {
    @Autowired
    private OrderMannerServiceImpI orderMannerServiceImpI;
    @Autowired
    private orderMapper orderMapper;
    @ApiOperation("订单搜索")
    @GetMapping("/conditionSearch")
    public Result conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        PageResult pageResult = orderMannerServiceImpI.serchOrder(ordersPageQueryDTO);
        return Result.success(pageResult);
    }
    @GetMapping("/statistics")
    @ApiOperation("各种订单状态统计")
    public Result<OrderStatisticsVO> statistics() {
        List<Map<String, Object>> statistics = orderMapper.statistics();
        OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO();
        Map<String, Consumer<Integer>> statusMap = new HashMap<>();
log.info(statistics.toString());
// 添加已有的状态处理方法
        statusMap.put("2", count -> orderStatisticsVO.setToBeConfirmed(count));
        statusMap.put("3", count -> orderStatisticsVO.setConfirmed(count));
        statusMap.put("4", count -> orderStatisticsVO.setDeliveryInProgress(count));
// 遍历 statistics 列表并处理每个状态
        for (Map<String, Object> row : statistics) {
            String status = String.valueOf(row.get("status")) ;
            Integer count = Math.toIntExact((Long) row.get("count"));
            // 使用 statusMap 获取对应的操作，如果没有对应的操作则默认什么也不做
            statusMap.getOrDefault(status, countVal -> {}).accept(count);
        }

        return Result.success(orderStatisticsVO);

    }


    @GetMapping("/details/{id}")
    @ApiOperation("查看订单详情")
    public Result<Orders> loookById(@PathVariable String id) {
        OrderVO details = orderMannerServiceImpI.details(id);
        return Result.success(details);
    }
    @PutMapping("/confirm")
    @ApiOperation("接单")
    public Result confirmOrder(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        orderMapper.updateStatus(ordersConfirmDTO);
        return Result.success();
    }
    @PutMapping("/rejection")
    @ApiOperation("拒单")
    public Result rejectionOrder(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {
        orderMannerServiceImpI.reject(ordersRejectionDTO);
        return Result.success();
    }
    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public Result cancelOrder(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception {
        orderMannerServiceImpI.cancelOrder(ordersCancelDTO);
        return Result.success();
    }
    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result completeOrder(@PathVariable String id) {
        Orders byId = orderMapper.findById(Integer.valueOf(id));
        byId.setStatus(Orders.COMPLETED);
        byId.setDeliveryTime(LocalDateTime.now());
        orderMapper.update(byId);
        return Result.success();
    }
    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单")
    public Result deliveryOrder(@PathVariable String id) {
        Orders byId = orderMapper.findById(Integer.valueOf(id));
        byId.setStatus(Orders.DELIVERY_IN_PROGRESS);
        orderMapper.update(byId);
        return Result.success();
    }
}
